package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.manager.Controller;

import common.utils.DensityUtil;
import common.utils.MyFrontUtil;


public class SetGenderDialog extends Dialog {
    private TextView resultTv;
    private int currentGender;
    private CommitDialogCallBack callback;
    private RadioButton setgender_girl_radio_btn;
    private RadioButton setgender_boy_radio_btn;
    private String male;
    private String female;

    private int dialogWidth;
    private String watchName;
    private Context context;

    /**
     * @param resultTv 修改完成后，显示结果的textview
     */
    public SetGenderDialog(Context context, int currentGender, TextView resultTv, CommitDialogCallBack callback) {
        super(context);
        this.resultTv = resultTv;
        this.currentGender = currentGender;
        this.callback = callback;
    }

    public SetGenderDialog(Context context, int currentGender, TextView resultTv, String watchName, CommitDialogCallBack callback) {
        super(context);
        this.resultTv = resultTv;
        this.currentGender = currentGender;
        this.callback = callback;
        this.watchName = watchName;
        this.context = context;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_setgender);

        male = getContext().getString(R.string.male);
        female = getContext().getString(R.string.female);

        setDialogAttributes();
        initDialogView();
    }

    private void initDialogView() {

        boolean oldManMode = Controller.getInstance().isOldMainMode();

        setgender_girl_radio_btn = (RadioButton) findViewById(R.id.setgender_girl_radio_btn);
        RadioGroup.LayoutParams layoutParams = (RadioGroup.LayoutParams) setgender_girl_radio_btn.getLayoutParams();
        layoutParams.leftMargin = (dialogWidth - DensityUtil.dip2px(getContext(), 239.5f)) / 3;
        setgender_girl_radio_btn.setLayoutParams(layoutParams);
        setgender_girl_radio_btn.setBackgroundResource(oldManMode ? R.drawable.selector_setgender_girl_bg3 : R.drawable.selector_setgender_girl_bg);


        setgender_boy_radio_btn = (RadioButton) findViewById(R.id.setgender_boy_radio_btn);
        RadioGroup.LayoutParams layoutParams2 = (RadioGroup.LayoutParams) setgender_boy_radio_btn.getLayoutParams();
        layoutParams2.leftMargin = layoutParams.leftMargin;
        setgender_boy_radio_btn.setLayoutParams(layoutParams2);
        setgender_boy_radio_btn.setBackgroundResource(oldManMode ? R.drawable.selector_setgender_boy_bg3 : R.drawable.selector_setgender_boy_bg);

        if (currentGender == 1) {
            setgender_boy_radio_btn.setChecked(true);
        } else {
            setgender_girl_radio_btn.setChecked(true);
        }
        if (watchName != null) {
            MyFrontUtil.initTextViewText(context, (TextView) findViewById(R.id.title_name_tv), watchName, context.getString(R.string.t_info_gender));
        }
        //初始化确定，取消按钮
        findViewById(R.id.cancel_btn).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    dismiss();
                }
                return false;
            }
        });

        findViewById(R.id.sure_btn).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {

                    resultTv.setText(setgender_boy_radio_btn.isChecked() ? male : female);

                    callback.onCommit();
                    dismiss();
                }
                return false;
            }
        });
    }


    public void setDialogAttributes() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        DisplayMetrics dm = new DisplayMetrics();
        dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

        dialogWidth = lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
        // lp.alpha = 0.9f; // 透明度

        dialogWindow.setAttributes(lp);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }


}
